package string.t459;

public class One {

    public boolean repeatedSubstringPattern(String s) {
        // 长度超过二分之一就不用了
        int begin = 0;
        int end = 0;


        while (end <= s.length() / 2) {
            if (isRepeat(s, begin, end)) {
                return true;
            } else {
                end++;
            }
        }

        return false;
    }

    private boolean isRepeat(String s, int begin, int end) {

        int sl = s.length();

        int repeatL = end - begin;

        if (sl % repeatL != 0) {
            return false;
        }

        if (repeatNext(s, begin, end)) {
            return true;
        }

        return false;

    }

    private boolean repeatNext(String s, int begin, int end) {
        if (begin > s.length() - 1 && end > s.length() - 1) {
            return false;
        }

        int oneEnd = end;

        while (begin <= oneEnd) {
            if (s.charAt(begin) != s.charAt(end + 1)) {
                return false;
            }
            begin++;
            end++;
        }

        if (end == s.length() - 1) {
            return true;
        }

        return repeatNext(s, oneEnd + 1, end);
    }
}
